﻿using SuperMap.Data;
using SuperMap.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MDF
{
    class DataInfo
    {
        private Datasources m_datasources;

        /// <summary>
        /// 选择数据类初始化
        /// </summary>
        public DataInfo() { }
        /// <summary>
        /// 选择数据类初始化
        /// </summary>
        /// <param name="datasources">数据源集合</param>
        public DataInfo(Datasources datasources) 
        {
            m_datasources = datasources;
        }
        /// <summary>
        /// 获取选择集的信息
        /// </summary>
        /// <param name="selectionsName"></param>
        /// <returns>返回数据集的名字和其所在的数据源的名字组成数组的列表</returns>
        public List<string[]> GetSelectionSetsInfo(string selectionsName)
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("SelectionData");
                //选择集中获取数据集的名字和其所在的数据源的名字
                List<string[]> set_sourceNames = m_Xml.ReadChildValues(selectionsName);
                return set_sourceNames;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 保存选择集信息
        /// </summary>
        /// <param name="map">选择集所在的地图</param>
        /// <param name="selectionsName">选择集名称</param>
        /// <returns>保存成功返回true，失败false</returns>
        public bool SaveSelectionsInfo(Map map,string selectionsName)
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("SelectionData");
                //获取选择集数组
                Selection[] selections = map.FindSelection(true);
                if (selections != null)
                {
                    //判断是否重名
                    if (m_Xml.ReadChildNodesName(selectionsName).Length == 0)
                    {
                        //遍历选择集，并写入信息
                        foreach (Selection selection in selections)
                        {
                            //获取数据集名称
                            string datasetName = selection.Dataset.Name;
                            //获取数据集所在数据源名称
                            string sourceName = selection.Dataset.Datasource.Alias;
                            //获取选中对象的id序列
                            string ids = "";
                            for (int i = 0; i < selection.Count; i++)
                            {
                                ids = ids + "、" + selection[i].ToString();
                            }
                            //保存选择集信息
                            m_Xml.WriteXml(selectionsName, datasetName,"sourceName", sourceName, ids);
                        }
                        //返回成功
                        return true;
                    }
                    //重名提醒
                    else
                    {
                        MessageBox.Show("已存在相同名称选择集！", "警告：");
                        return false;
                    }
                }
                else
                {
                    MessageBox.Show("当前地图选择集为空", "警告：");
                    return false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除选择集信息
        /// </summary>
        /// <param name="selectionsName">选择集的名字</param>
        /// <returns></returns>
        public bool DeleteSelectionsInfo(string selectionsName)
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("SelectionData");
                //判断是否存在
                if (m_Xml.ReadChildNodesName(selectionsName).Length != 0)
                {
                    if (m_Xml.DeleteXml(selectionsName))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    MessageBox.Show("该选择集信息不存在！", "警告：");
                    return false;
                }
                

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取所有选择集的名称
        /// </summary>
        /// <returns>选择集的名称</returns>
        public string[] GetAllSelectionsNames()
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("SelectionData");
                //获取所有选择集的名称
                string[] selectionsName = m_Xml.ReadChildNodesName("SelectionData");
                //返回结果
                return selectionsName;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取选择集中的数据集信息
        /// </summary>
        /// <param name="selectionsName">选择集的名字</param>
        /// <param name="datasetName">数据集的名字</param>
        /// <returns>返回id数组</returns>
        public int[] GetSelectionDataIDs(string selectionsName,string datasetName)
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("SelectionData");
                //获取ids文本
                string idsText = m_Xml.ReadInnerText(selectionsName,datasetName);
                string[] idsStr = idsText.Remove(0, 1).Split('_');
                int[] IDs = Array.ConvertAll(idsStr, int.Parse);
                //返回结果
                return IDs;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取选择集的描述信息
        /// </summary>
        /// <param name="selectionsName"></param>
        /// <returns>返回选择集信息</returns>
        public string GetSelectionsInfo(string selectionsName)
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("SelectionData");
                //获取选择集的信息
                StringBuilder stringBuilder = new StringBuilder();
                //第一行为选择集的名字
                stringBuilder.AppendLine(selectionsName);
                //第二行获取数据集的名字和其所在的数据源的名字
                List<string[]> set_sourceNames = m_Xml.ReadChildValues(selectionsName);
                foreach (string[] set_sourceName in set_sourceNames)
                {
                    string names = "  数据集：" + set_sourceName[0] + ">>>" + set_sourceName[1];
                    stringBuilder.AppendLine(names);
                    //第三行获取选择对象的id数组
                    string innerText = m_Xml.ReadInnerText(selectionsName, set_sourceName[0]).Replace("_", "、").Remove(0,1);
                    string selectionIDs = "  已选择对象ID:" + innerText;
                    stringBuilder.AppendLine(selectionIDs);
                }
                return stringBuilder.ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取数据集相关信息
        /// </summary>
        /// <param name="datasetName">数据集名称</param>
        /// <param name="infoName">xml文件信息节点名</param>
        /// <param name="andStr">名称连接词</param>
        /// <returns>返回数据集信息</returns>
        public string GetDatasetsInfo(string datasetName,string infoName, string andStr)
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("PiPeiYuZhi");
                //获取数据集信息（不建议有同名数据集存在）
                StringBuilder stringBuilder = new StringBuilder();
                //第一行填充名字
                stringBuilder.AppendLine(datasetName);
                DatasetVector datasetVector = null;
                foreach (Datasource datasource in m_datasources)
                {
                    if (datasource.Datasets.Contains(datasetName))
                    {
                        //获取数据后跳出
                        datasetVector = datasource.Datasets[datasetName] as DatasetVector;
                        break;
                    }
                }
                if (datasetVector != null)
                {
                    //后续行填充数据集的基本信息
                    stringBuilder.AppendLine("  数据源:" + datasetVector.Datasource.Alias);
                    stringBuilder.AppendLine("  数据集类型:" + datasetVector.Type.ToString());
                    stringBuilder.AppendLine("  要素数量:" + datasetVector.RecordCount.ToString());
                    stringBuilder.AppendLine("  坐标单位:" + datasetVector.PrjCoordSys.CoordUnit.ToString());

                    //最后填充阈值信息或映射信息（如果有）
                    string[] infos = m_Xml.ReadChildNodesName(infoName);
                    foreach (string info in infos)
                    {
                        if (info.Contains(datasetName))
                        {
                            string[] values = m_Xml.ReadValues(infoName, info);
                            stringBuilder.AppendLine("阈值信息:" + info.Replace(andStr, "<>"));
                            if(infoName == "KongJianYuZhi")
                            {
                                stringBuilder.AppendLine("  距离阈值:" + values[0]);
                                stringBuilder.AppendLine("  长度误差:" + values[1]);
                            }
                            else if(infoName == "TuoPuYuZhi")
                            {
                                stringBuilder.AppendLine("  保留阈值:" + values[0]);
                                stringBuilder.AppendLine("  节点容限:" + values[1]);
                                stringBuilder.AppendLine("  搜索深度:" + values[2]);
                            }
                            else
                            {
                                stringBuilder.Clear();
                                stringBuilder.AppendLine("映射信息:" + info.Replace(andStr, "<>"));
                                //获取映射信息
                                List<string[]> childinfos = m_Xml.ReadChildValues(info);
                                foreach (string[] childinfo in childinfos)
                                {
                                    stringBuilder.AppendLine("  " + childinfo[0] + ": " + childinfo[1] + "=" + childinfo[2]);
                                }
                                
                            }
                        }
                    }
                }
                return stringBuilder.ToString();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取点数据链接的线数据集合
        /// </summary>
        /// <param name="datasetName">点数据集名称</param>
        /// <returns>返回点线数据集合</returns>
        public DatasetVector[] GetLinkDatasets(string datasetName)
        {
            try
            {
                //实例化xml读写对象
                IO_xml m_Xml = new IO_xml("PiPeiYuZhi");
                //获取数据集信息（不建议有同名数据集存在）
                string[] linesetNames = m_Xml.ReadValues("TuoPuLink", datasetName);
                List<DatasetVector> datasetVector = new List<DatasetVector>();
                foreach (string linesetName in linesetNames)
                {
                    foreach (Datasource datasource in m_datasources)
                    {
                        if (datasource.Datasets.Contains(linesetName))
                        {
                            //获取数据后跳出
                            datasetVector.Add(datasource.Datasets[linesetName] as DatasetVector);
                            break;
                        }
                    }
                }
                return datasetVector.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
